package com.sigmatrix.h5.web.ins.controller;

import javax.servlet.http.Cookie;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sigmatrix.h5.auth.H5Authenticator;
import com.sigmatrix.h5.enums.SysResModule;
import com.sigmatrix.h5.exception.MessageException;
import com.sigmatrix.h5.properties.CookieProperties;
import com.sigmatrix.h5.service.UsersService;
import com.sigmatrix.h5.utils.Assert;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.web.BaseController;
import com.sigmatrix.h5.web.Result;
import com.sigmatrix.h5.web.args.LoginParam;
import com.sigmatrix.h5.web.mode.Login;
import com.sigmatrix.h5.web.mode.VerCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 *@ClassName: UsersController
 *@Description: 用户登录
 *@author MH
 *@date 2017年9月22日 上午11:34:25
 */
@Api(tags="平台账号登录")
@RestController
@RequestMapping("/ins/user")
public class UsersController extends BaseController{
	
	@Autowired
	private H5Authenticator authenticator; 
	
	@Autowired
	UsersService usersService;
	
	private String COOKIE_DOMAIN;
	
	private String VER_CODE_COOKIE_KEY;
	
	@Value("${spring.application.name}")
	private String applicationName;
	
	@Autowired(required=false)
	UsersController(ConfigClientProperties configClientProperties,CookieProperties cookieProperties) {
		VER_CODE_COOKIE_KEY = configClientProperties.getProfile()+"VerCode";
		if(cookieProperties != null) COOKIE_DOMAIN = cookieProperties.getDomain();
	}
	/**
	 * 
	 *@Title: login
	 *@Description: 登录
	 *@return Result
	 *@author MH
	 *@date 2017年9月22日 下午6:35:33
	 *@param loginName
	 *@param loginPwd
	 *@return
	 * @throws MessageException 
	 */
	@ApiOperation("平台账号登录")
	@PostMapping("login")
	public Result<Login> login(@RequestBody LoginParam vo){
		if(CommonUtils.isEmpty(vo)
				||StringUtils.isBlank(vo.getLoginName()) 
				|| StringUtils.isBlank(vo.getLoginPwd())){
			return error("账号/密码不能为空");
		}
		boolean b = vilidateVerificationCode(vo.getVerificationCode());
		if(!b){
			return error("验证码错误");
		}
		
		LoginParam param = new LoginParam();
		param.setLoginName(vo.getLoginName());
		param.setLoginPwd(vo.getLoginPwd());
		param.setLoginType(SysResModule.INSPECTION.code);
		Login login = (Login) authenticator.login(param);
		
		return ok(login);
	}
	@ApiOperation("登出")
	@PostMapping("logout")
	public Result<Void> logout(String token){
		authenticator.logout(token);
		return ok(null);
	}
	
	/**
	 * 
	 *@Title: verilicationCode
	 *@Description: 生成验证码
	 *@return Result
	 *@author MH
	 *@date 2017年10月25日 上午9:21:17
	 *@return
	 */
	@ApiOperation(value="生成验证码图片",response=VerCode.class)
	@PostMapping("vercode")
	public Result<VerCode> verilicationCode(){
		VerCode code = usersService.generateVerCode();
		authenticator.cacheVcode(code.getValue());
		
		return ok(code);
	}
	/**
	 * 
	 *@Title: vilidateVerificationCode
	 *@Description: 校验验证码
	 *@return boolean
	 *@author MH
	 *@date 2017年10月24日 下午6:08:16
	 *@param verificationCode
	 *@return
	 */
	private boolean vilidateVerificationCode(String verificationCode){
		Assert.notNull(verificationCode, "验证码不能为空");
		return authenticator.verifyVCode(verificationCode);
	}
}
